function CityController(){ }
 
CityController.prototype.params = {};

CityController.prototype.handleMapFooterEvent = function() {
    Util.prototype.showSpinner();
    var curPageId = $.mobile.activePage.attr("id");
    if (curPageId == "pagePrefArchives") { // 日本地図
        PageChangeController.prototype.changeToPage('pageJapanMap');
    } else // グーグル地図
    if (curPageId == "pageCityArchives") { 
        var params = {'localName': CityController.prototype.params.cityName};
        PageChangeController.prototype.changeToPage('pageGMap', params);
    } else if (curPageId == "pageBookmarkList") {
        var params = {'localName': BookmarkListController.prototype.params.cityName};
        PageChangeController.prototype.changeToPage('pageGMap', params);
    } else {
        Util.prototype.hideSpinner();
    }
};

CityController.prototype.freeMemory = function(isPageHome) {
    $('#cityList').html('').listview('refresh');
    $('#prefBookmarkList').html("").listview('refresh');
    if (isPageHome) {
        CityController.prototype.params = null;
    }
};

$(function(){
    $(document).on('pagebeforeshow', '#pageCityArchives', function(){
        var cityCode = CityController.prototype.params.cityCode,
            cityName = CityController.prototype.params.cityName;
        
        monaca.updateUIStyle("titlelable", "text", cityName);
        monaca.updateUIStyle("footer", "visibility", "true");
        Util.prototype.showEditIcon(cityName);
        
        Util.prototype.changeSnapperElement("cityarchives-content");
 
        var listview = $('#prefBookmarkList');
        listview.append("<li data-role='list-divider'><span class='spanpref'>"+cityName+"</span>  の一覧</li>");

        BookmarksService.prototype.countProductByCity(cityName, function(results) {
            var len = results.rows.length,
                list = '';
                        
            for (var i=0; i<len; i++){
                var item = results.rows.item(i);
                if (i == 0){
                    list += "<li><a href='#' id = 'city" + item.pref_no + "' class = 'bookmarkCity' city = " + item.pref_name + ">"+
                            item.pref_name + " の一覧を表示<span class='ui-li-count'>" + item.citykazu+"</span></a></li>";    
                } else {
                    list += "<li><a href='#' id = 'city" + item.code + "' class = 'bookmarkCity' city = " + item.city_name + ">"+
                            item.city_name + "<span class='ui-li-count'>" + item.citykazu+"</span></a></li>";       
                }
            }
                
            $('#cityList').append(list).listview('refresh');
        });
        
        // Load bookmark list
        ProductListLoader.prototype.initLoadCityBookmark(cityCode);
        ProductListLoader.prototype.loadProducts(listview, $(".cityarchives-pull"), null, "<li><h3>商品が見つかりませんでした</h3></li>");
    });
    
    $(document).on('pageshow', '#pageCityArchives', function(){
        PageChangeController.prototype.freeMemory();
    });
    
    // 市町村別リストクリック
    // クリックされたキーワードのブックマーク一覧を表示
    $(document).on('tap', '.bookmarkCity', function(){
        Util.prototype.showSpinner();
        var cityCode = $(this).attr('id').slice(4),
            cityName = $(this).attr('city');
        var params = {'title': cityName, 'fromPage': 'pageCityArchives','cityCode': cityCode, 'prefCode': CityController.prototype.params.cityCode};
        PageChangeController.prototype.changeToPage("pageBookmarkList", params);

        return false;
    });

});